package com.siruier.boss.ui.activity.mall

import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.recyclerview.widget.GridLayoutManager
import com.google.android.material.appbar.AppBarLayout
import com.kingja.loadsir.core.LoadService
import com.siruier.boss.api.MALL_API_SERVICE
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.mallApi
import com.siruier.boss.bean.MallCategoryBean
import com.siruier.boss.bean.MallCategorySimpleBean
import com.siruier.boss.bean.MallGoodsBean
import com.siruier.boss.common.GOODS_SPAN_COUNT
import com.siruier.boss.databinding.ActivityCategoryDetailBinding
import com.siruier.boss.databinding.ActivityStoreBinding
import com.siruier.boss.ui.adapter.GoodsAdapter
import com.siruier.boss.ui.api.ApiObserver
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.ext.showLoadingCallback
import com.siruier.boss.ui.ext.tabData
import com.siruier.boss.ui.helper.InjectBundle
import com.siruier.boss.ui.helper.RefreshHelper
import com.siruier.boss.ui.helper.loadsir.LoadSirHelper
import com.siruier.boss.ui.listener.OnTabSelectedListener
import com.siruier.boss.ui.utils.Utils
import java.util.ArrayList

class CategoryDetailActivity : BaseActivity<ActivityCategoryDetailBinding>() {
    private lateinit var mRefreshHelper: RefreshHelper<MallGoodsBean>
    private val mGoodsAdapter = GoodsAdapter()
    private var mLayoutManager: GridLayoutManager? = null
    private val categoryId: Int by InjectBundle()
    override fun initView() {
        bindViewClick(vb.ivToUp)
        mRefreshHelper =
            RefreshHelper(vb.refreshLayout, vb.rvGoods, mGoodsAdapter, listener = ::onReload)
        mLayoutManager = GridLayoutManager(this, GOODS_SPAN_COUNT)
        vb.rvGoods.layoutManager = mLayoutManager
        vb.searchView.clearListener = ::onReload
        mRefreshHelper.onRefresh()
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.ivToUp -> {
                mLayoutManager?.scrollToPositionWithOffset(0, 0)
            }
        }
    }

    private fun onReload() {
        Utils.hideSoftInput(this)
        launchUI({
            MALL_API_SERVICE.goodsList(vb.searchView.text, categoryId, mRefreshHelper.page)
                .toData()?.records
        }, mRefreshHelper.handApiObserver())
    }
}